ListDelayBindItem = class('ListDelayBindItem', GUIView)

function ListDelayBindItem:InitPanel()
    GUIView.InitPanel(self)
    self.rectTransform = self.gameObject:GetComponent('RectTransform')
    self.needUpdateVisibility = true
end

function ListDelayBindItem:OnShow()
    self.delayUpdate = TIMERM.Step(function()
        self.bindingUpdate = TIMERM.Step(self.UpdateBinding, self, 10, -1)
    end, nil, math.random(1, 10), 1)
end

function ListDelayBindItem:OnHide()
    if self.delayUpdate then
        self.delayUpdate.remove = true
    end
    self.delayUpdate = nil
    TIMERM.Kill(self.UpdateBinding, self)

    self.bindLaterViewModel = nil
    GUIView.OnHide(self)
end

function ListDelayBindItem:InitPanelJIT() end
function ListDelayBindItem:Set_BindingContext(viewModel)
    if viewModel then
        self:InitPanelJIT()
        GUIView.OnShow(self)
    end
    GUIView.Set_BindingContext(self, viewModel)

    if self.delayUpdate then
        self.delayUpdate.remove = true
    end
    self.delayUpdate = nil
    TIMERM.Kill(self.UpdateBinding, self)

    self.bindLaterViewModel = nil
end

function ListDelayBindItem:UpdateBinding()
    local calcBound = function (rectTransform)
        local min, max
        local worldCorners = rectTransform:GetWorldCorners()
        for i = 0, 3 do
            local worldCorner = worldCorners[i]
            local screenPoint = VIEWM.GuiCameraComponent:WorldToScreenPoint(worldCorner)
            min = min == nil and screenPoint or Vector3.Min(min, screenPoint)
            max = max == nil and screenPoint or Vector3.Max(max, screenPoint)
        end

        return Bounds.New((max + min) / 2, max - min)
    end

    if self.transform.hasChanged then
        self.transform.hasChanged = false
        self.needUpdateVisibility = true
    end

    if self.needUpdateVisibility then
        local scrollRect = self.transform:GetComponentInParent(typeof(UnityEngine.UI.ScrollRect))
        local maskTrans = scrollRect.viewport:GetComponent('RectTransform')

        local maskBound = calcBound(maskTrans)
        local bound = calcBound(self.rectTransform)

        if maskBound:Intersects(bound) then
            self:Set_BindingContext(self.bindLaterViewModel)
            LateUpdateBeat:Remove(self.UpdateBinding, self)
        end
        self.needUpdateVisibility = false
    end
end